<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <el-form :inline="true" :model="query" @keyup.enter.native="toQuery()">
        <!-- 搜索 -->
        <el-form-item label="用户id：">
          <el-input v-model="query.uid" placeholder="用户id" clearable style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item label="用户名：">
          <el-input v-model="query.name" placeholder="用户名" clearable style="width: 150px"></el-input>
        </el-form-item>
        <el-form-item label="状态：">
          <el-select v-model="query.status" placeholder="状态" clearable style="width: 140px">
            <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
          </el-select>
        </el-form-item>
        <!--      <div class="block">-->
        <span class="demonstration">选择时间：</span>
        <el-date-picker
          v-model="chooseDate"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right"
          clearable
        >
        </el-date-picker>
        <el-form-item style="margin-left: 1%">
          <el-button type="primary" @click="toQuery()" v-has-permission="['ums:extract:list']">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="resetQuery()">刷新</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="info"
            icon="el-icon-download"
            size="small"
            @click="handleExport"
            v-has-permission="['ums:extract:export']"
          >导出
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permission="['ums:extract:delete']"
          >批量删除
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--表单组件-->
    <!--    <eForm ref="form" :is-add="isAdd"/>-->
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" border style="width: 100%;"
              @selection-change="handleSelectionChange" :header-cell-style="{background:'#EAE9E9FF'}">
      <el-table-column type="selection" width="50px"/>
      <el-table-column prop="realName" label="代理账户" width="90%"/>
      <el-table-column prop="bankName" label="银行名称"/>
      <el-table-column prop="bankAddress" label="开户行" width="180px"/>

      <el-table-column prop="bankCode" label="银行卡号" width="180px"/>
      <el-table-column prop="extractPrice" label="提现金额/(元)" width="100px"/>
      <el-table-column prop="applyTime" label="申请时间"></el-table-column>
      <el-table-column prop="extractType" label="提现方式" width="90%">
        <template slot-scope="scope">
          <div v-if="scope.row.extractType=== 0">银行卡</div>
          <div v-if="scope.row.extractType=== 2">微信</div>
          <div v-if="scope.row.extractType=== 1">支付宝</div>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="审核备注" width="80%"/>
      <el-table-column prop="examineTime" label="审核时间"></el-table-column>
      <el-table-column prop="status" label="审核状态" width="80px">
        <template slot-scope="scope">
          <div v-if="scope.row.status === 1" style="color: #13ce66;font-weight: bold">
            已处理
          </div>
          <div v-if="scope.row.status === -1" style="color: black;">
            已驳回
          </div>
          <div v-if="scope.row.status === 0" style="color: red;font-weight: bold">
            待审核
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="管理"
        align="center"
        width="240px"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="success(scope.row.id)" v-if="scope.row.status === 0" v-has-permission="['ums:extract:success']">通过
          </el-button>
          <el-button size="mini" type="danger" @click="reject(scope.row.id)" v-if="scope.row.status === 0" v-has-permission="['ums:extract:reject']">驳回
          </el-button>
          <el-button size="mini" type="primary" @click="showExtractDetail(scope.row.id)" v-has-permission="['ums:extract:info']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
    <show-extract-detail v-if="showVisible" ref="showExtractDetail"
                         @refreshDataList="toQuery"
    ></show-extract-detail>
  </div>
</template>

<script>
import showExtractDetail from './showExtractDetail'
import checkPermission from '@/utils/permission'
import initData from '@/mixins/crud'
import { delUserExtract, exportUserExtract, rejectExtract, success } from '@/api/ums/userExtract'
// import eForm from './form'
import { downloadFile, formatTimeTwo } from '@/utils/index'

export default {
  components: { showExtractDetail },
  mixins: [initData],
  data() {
    const shortcuts = {
      shortcuts: [{
        text: '最近一周',
        onClick(picker) {
          const end = new Date()
          const start = new Date()
          end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
          picker.$emit('pick', [start, end])
        }
      }, {
        text: '最近一个月',
        onClick(picker) {
          const end = new Date()
          const start = new Date()
          end.setTime(start.getTime() + 3600 * 1000 * 24 * 30)
          picker.$emit('pick', [start, end])
        }
      }, {
        text: '最近三个月',
        onClick(picker) {
          const end = new Date()
          const start = new Date()
          end.setTime(start.getTime() + 3600 * 1000 * 24 * 90)
          picker.$emit('pick', [start, end])
        }
      }]
    }
    return {
      showVisible: false,
      delLoading: false,
      // 非多个禁用
      multiple: true,
      chooseDate: [],
      pickerOptions: shortcuts,
      queryTypeOptions: [
        { key: '-1', display_name: '审核拒绝' },
        { key: '0', display_name: '待审核' },
        { key: '1', display_name: '审核成功' }
      ],
      selectionId: [],
      dataForm: {
        id: '',
        uid: '',
        realName: '',
        extractName: '',
        extractType: '',
        bankName: '',
        bankCode: '',
        bankAddress: '',
        extractPrice: '',
        failMsg: '',
        status: '',
        createTime: '',
        updateTime: '',
        applyTime: '',
        examineTime: '',
        remark: '',
        isDelete: ''
      }
    }
  },

  created() {
    this.$nextTick(() => {
      this.query.status = this.$route.query.status
      this.init()
    })
  },

  methods: {
    formatTimeTwo,
    checkPermission,
    beforeInit() {
      this.url = 'admin/api/userExtract/list'
      this.params = {
        uid: this.query.uid,
        name: this.query.name,
        status: this.query.status,
        startTime: this.chooseDate == null ? '' : this.chooseDate[0],
        endTime: this.chooseDate == null ? '' : this.chooseDate[1]
      }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) {
        this.params[type] = value
      }
      return true
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delUserExtract(ids)
      }).then(() => {
        // this.$refs[id].doClose()
        this.toQuery()
        this.$message.success('操作成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const dataForm = this.dataForm
      delete dataForm.pageNum
      delete dataForm.pageSize
      this.$confirm('是否确认导出所有提现数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportUserExtract(dataForm)
      }).then(response => {
        downloadFile(response.data, '提现记录数据', 'xls')
      })
    },
    showExtractDetail(id) {
      this.showVisible = true
      this.$nextTick(() => {
        this.$refs.showExtractDetail.init(id)
      })
    },
    reject(id) {
      this.$confirm('是否要驳回编号为"' + id + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return rejectExtract(id)
      }).then(() => {
        // this.$refs[id].doClose()
        this.toQuery()
        this.$message.success('操作成功')
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.query = {}
      this.chooseDate = []
      this.toQuery()
    },
    success(id) {
      this.$confirm('是否通过该申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return success(id)
      }).then(() => {
        // this.$refs[id].doClose()
        this.toQuery()
        this.$message.success('操作成功')
      })
    }
  }
}
</script>

<style scoped>
/deep/ .el-table th > .cell {
  text-align: center;
}

/deep/ .el-table .cell {
  text-align: center;
}
</style>
